package com.gxa.saas.controller;


import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.gxa.saas.pojo.VisitCount;
import com.gxa.saas.pojo.VisitResult;
import com.gxa.saas.service.VisitCountService;
import com.gxa.saas.service.VisitResultService;
import com.gxa.saas.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Api("拜访的相关操作")
@RestController
@RequestMapping("/visit")
@CrossOrigin
public class VisitController {
    @Autowired
    VisitResultService visitResultService;

    @Autowired
    VisitCountService visitCountService;

    @ApiOperation(value = "分页查询")
    @PostMapping("/selectByPage")
    public Map selectByPage(Integer page){

        Integer size=3;
        List<VisitResult> visitResults = visitResultService.selectAll();
        List<VisitResult> visitResults1 = visitResultService.selectByPage(page, size);


        JsonResult jsonResult=new JsonResult();
        jsonResult.setData(visitResults1);
        jsonResult.setMsg("");
        jsonResult.setCode("200");
        jsonResult.set("total",visitResults.size());
        jsonResult.set("pageSize",size);

        return jsonResult.getMap();
    }


    @ApiOperation(value = "全部导出")
    @PostMapping("/exportAll")
    public Map exportAll(HttpServletResponse response) throws Exception {
        List<VisitResult> visitResults = visitResultService.selectAll();
        visitResultService.export(visitResults,response);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        return  jsonResult.getMap();
    }

    @ApiOperation("部分导出")
    @PostMapping("/exportPart")
    public Map exportPart(HttpServletResponse response,Integer... id) throws  Exception{

        List<VisitResult> list=new ArrayList();

        for (int i = 0; i < id.length; i++) {
            VisitResult visitResult = visitResultService.selectById(id[i]);
            if(visitResult!=null){
                list.add(visitResult);
            }
        }

        visitResultService.export(list,response);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        return  jsonResult.getMap();
    }
    @ApiOperation(value = "拜访统计分页查询")
    @PostMapping("/selectByPage2")
   public Map selectByPage2(Integer page){
        Integer size=3;
        List<VisitCount> visitCounts = visitCountService.selectByPage(page,size);
        List<VisitCount> visitCounts1 = visitCountService.selectAll();


        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        jsonResult.setData(visitCounts);
        jsonResult.set("total",visitCounts1.size());
        jsonResult.set("pageSize",size);

        return jsonResult.getMap();
    }


    @ApiOperation(value = "根据条件查询")
    @PostMapping("/selectByName")
    public Map selectByName(String from,String to,String contactPerson,String name){
        Map map=new HashMap();
        map.put("name",name);
        map.put("from",from);
        map.put("to",to);
        map.put("contactPerson",contactPerson);
        List<VisitResult> visitResults = visitResultService.selectByName(map);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        jsonResult.setData(visitResults);

        return jsonResult.getMap();
    }

    @ApiOperation(value = "拜访统计全部导出")
    @PostMapping("/exportAll2")
    public Map exportAll2(HttpServletResponse response) throws Exception {
        List<VisitCount> visitCounts = visitCountService.selectAll();
        visitCountService.export(visitCounts,response);


        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        return  jsonResult.getMap();
    }


    @ApiOperation(value = "拜访统计部分导出")
    @PostMapping("/exportPart2")
    public Map exportPart2(HttpServletResponse response,Integer... id) throws Exception {

        List<VisitCount> list=new ArrayList();
        for (int i = 0; i < id.length; i++) {
            VisitCount visitCount = visitCountService.selectById(i);
            if(visitCount!=null){
                list.add(visitCount);
            }

        }

        visitCountService.export(list,response);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        return  jsonResult.getMap();

    }



    @ApiOperation(value = "拜访统计根据条件差查询")
    @PostMapping("/selectByName2")
    public Map selectByName2(String from,String to,String name,String type,String person,Integer effectiveTime,Integer visitTime){

        Map map=new HashMap<>();
        map.put("name",name);
        map.put("type",type);
        map.put("effectiveTime",effectiveTime);
        map.put("visitTime",visitTime);
        map.put("from",from);
        map.put("to",to);
        map.put("person",person);

        List<VisitCount> visitCounts = visitCountService.selectByName(map);



        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        jsonResult.setData(visitCounts);

        return jsonResult.getMap();
    }

    @ApiOperation(value = "查询有效拜访次数")
    @PostMapping("/effectiveTime")
    public Map selectEffective(String name){
        List<VisitResult> visitResults = visitResultService.selectByEffective(name);

        JsonResult jsonResult=new JsonResult();
                jsonResult.setCode("200");
                jsonResult.setData(visitResults);
                return  jsonResult.getMap();
    }


    @ApiOperation(value = "查询拜访次数")
    @PostMapping("/visitTime")
    public Map selectVisitTime(String name){
        List<VisitResult> visitResults = visitResultService.selectByVisit(name);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(visitResults);

        return  jsonResult.getMap();
    }

}
